import axios from "axios";
import { baseUrl } from '@/config/index.js'
import router from '@/router/index.js'
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 300000,
});

service.interceptors.request.use(
  async (config) => {
    config.headers.Authorization = 'Bearer ' + localStorage.getItem("token");
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  (response) => {
    if(response.data.code===200){
      return Promise.resolve(response.data)
    }else if(response.data.code===401){
      localStorage.removeItem('token')
      router.replace('/login')
    }else{
      return Promise.reject(response.data)
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
